﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChicagoDealsN.WebUserControls
{
    public partial class Login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                SetCurrentState();
        }

        public void btnLogin_OnClick(object sender, EventArgs e)
        {
            DataUser loggedUser = BizUser.Login(txtUsername.Text.Trim(), txtPassword.Text.Trim());
            if (loggedUser != null)
            {
                Session.Add("UserDet", loggedUser);
            }
            else
            {
                confirmation.Text = "UserName and Password donot match";
            }
            SetCurrentState();
        }
        public void btn_Logout_OnClick(object sender, EventArgs e)
        {
            Session.Contents.Remove("UserDet");
            confirmation.Text = "Logged out!";
            SetCurrentState();
            Response.Redirect("~/website/Default.aspx");
        }

        private void SetCurrentState()
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            if (Session["UserDet"] != null)
            {
                DataUser userInfo = Session["UserDet"] as DataUser;
                lbl_UserName.Text = userInfo.User_Name;
                pnl_loginPanel.Visible = false;
                pnl_loggedInPanel.Visible = true;
            }
            else
            {
                pnl_loginPanel.Visible = true;
                pnl_loggedInPanel.Visible = false;
            }
            Page.DataBind();
        }
    }
}